﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_NavBar:ContainerBase
    {
        private List<NavBarItemConfig> _pages = new List<NavBarItemConfig>(4);

        public PageCommon_NavBar(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-tab");
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="conofigFunc"></param>
        /// <returns></returns>
        public PageCommon_NavBar AddItem(Action<NavBarItemConfig> conofigFunc)
        {
            if (conofigFunc == null)
            {
                throw new ArgumentNullException(nameof(conofigFunc));
            }

            var config = new NavBarItemConfig();

            conofigFunc(config);

            _pages.Add(config);

            return this;
        }

        public NavBarPage RenderPage(string pageID)
        {
            var page = new NavBarPage(pageID, (_pages.FirstOrDefault(x => x._href == "#" + pageID)?._isActived).GetValueOrDefault(false), ViewContext);

            return (NavBarPage)page;
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end tab__bd -->");

            ViewContext.Writer.WriteLine("</div>");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.Writer.WriteLine($"<div {_attrs} >");

            ViewContext.Writer.WriteLine($"<div class=\"weui-navbar\">");

            foreach (var config in _pages)
            {
                ViewContext.Writer.WriteLine($"<a href=\"{config._href}\" class=\"weui-tabbar__item {(config._isActived ? "weui-bar__item--on" : "")}\">\n<span class=\"weui-badge\" style=\"position: absolute;top: -.4em;right: 1em;{(config._badge <= 0 ? "display:none;" : "")}\">{config._badge}</span>\n<div class=\"weui-tabbar__icon\">\n<img src=\"{config._icon}\" alt=\"\">\n</div>\n<p class=\"weui-tabbar__label\">{config._title}</p>\n</a>");
            }

            ViewContext.Writer.WriteLine($"</div> <!-- end weui-navbar -->");
            
            ViewContext.Writer.WriteLine("<div class=\"weui-tab__bd\">");
            
            return this;
        }

        public class NavBarItemConfig
        {
            internal string _title;
            internal string _icon;
            internal int _badge = 0;
            internal string _href = "";
            internal bool _isActived = false;

            /// <summary>
            /// 按钮标题
            /// </summary>
            /// <param name="title"></param>
            /// <returns></returns>
            public NavBarItemConfig Title(string title)
            {
                _title = title;

                return this;
            }

            /// <summary>
            /// 按钮图标链接
            /// </summary>
            /// <param name="logoUrl"></param>
            /// <returns></returns>
            public NavBarItemConfig Icon(string logoUrl)
            {
                _icon = logoUrl;

                return this;
            }

            /// <summary>
            /// 右上角显示的角标数量,为0时不显示
            /// </summary>
            /// <param name="badge"></param>
            /// <returns></returns>
            public NavBarItemConfig Badge(int badge)
            {
                _badge = badge;

                return this;
            }

            /// <summary>
            /// 点击后,显示指定ID的tab子页面,与Href二选一
            /// </summary>
            /// <param name="pageID"></param>
            /// <returns></returns>
            public NavBarItemConfig NavToPageID(string pageID)
            {
                _href = "#" + pageID;

                return this;
            }

            /// <summary>
            /// 点击按钮后跳转到指定链接,与NavToPageID二选一
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public NavBarItemConfig Href(string url)
            {
                _href = url;

                return this;
            }

            /// <summary>
            /// 是否为当前选项卡
            /// </summary>
            /// <param name="isActive"></param>
            /// <returns></returns>
            public NavBarItemConfig Active(bool isActive = true)
            {
                _isActived = isActive;

                return this;
            }
        }

        public class NavBarPage : ContainerBase
        {
            private string _id = "";
            private bool _active = false;
            private string _callOnRefresh = "";
            private string _callOnLoadMore = "";

            internal NavBarPage(string pageID, bool isActive, ViewContext viewContext) : base(viewContext)
            {
                _active = isActive;

                this.ID(pageID);
                this.Css("weui-tab__bd-item");
            }

            public NavBarPage EnablePullToRefresh(string callOnRefresh)
            {
                _callOnRefresh = callOnRefresh;

                return this;
            }

            public NavBarPage EnableLoadingMore(string callOnLoadMoreTrigger)
            {
                _callOnLoadMore = callOnLoadMoreTrigger;
                return this;
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end  weui-tab__bd-item-->");

            }

            internal override ContainerBase RenderInternal()
            {
                if (!string.IsNullOrWhiteSpace(_callOnRefresh))
                {
                    this.Data("onpulltorefresh", _callOnRefresh);

                    this.Css("pullToRefreshCollection");
                }

                if (!string.IsNullOrWhiteSpace(_callOnLoadMore))
                {
                    this.Data("loadmore", "True");
                    this.Data("loadmorecallback", _callOnLoadMore);
                }

                if (_active)
                {
                    this.Css("weui-tab__bd-item--active");
                }

                ViewContext.Writer.WriteLine($"<div {_attrs}");

                return this;
            }
        }
    }
}